package com.ruoyi.supply.controller;

import com.ruoyi.supply.domain.dto.ConsumeLogSearchDto;
import com.ruoyi.supply.service.impl.MemberLogService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import java.util.List;
import javax.servlet.http.HttpServletResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.supply.domain.TMember;
import com.ruoyi.supply.service.impl.TMemberService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 会员管理Controller
 * 
 * @author Ryan
 * @date 2025-06-13
 */
@RestController
@RequestMapping("/supply/member")
@Api(tags = "会员管理")
public class TMemberController extends BaseController
{
    @Autowired
    private TMemberService tMemberService;

    @Autowired
    private MemberLogService memberLogService;


    /**
     * 查询会员管理列表
     */
    @GetMapping("/list")
    @ApiOperation("查询会员管理")
    public TableDataInfo list(TMember tMember)
    {
        startPage();
        List<TMember> list = tMemberService.selectTMemberList(tMember);
        return getDataTable(list);
    }

    /**
     * 导出会员管理列表
     */
    @Log(title = "会员管理", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, TMember tMember)
    {
        List<TMember> list = tMemberService.selectTMemberList(tMember);
        ExcelUtil<TMember> util = new ExcelUtil<TMember>(TMember.class);
        util.exportExcel(response, list, "会员管理数据");
    }

    /**
     * 获取会员管理详细信息
     */
    @GetMapping(value = "/{id}")
    @ApiOperation("获取会员管理详细信息")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return success(tMemberService.selectTMemberById(id));
    }


    /**
     * 修改会员管理
     */
    @Log(title = "会员管理", businessType = BusinessType.UPDATE)
    @PutMapping
    @ApiOperation("修改会员管理")
    public AjaxResult edit(@RequestBody TMember tMember)
    {
        return toAjax(tMemberService.updateTMember(tMember));
    }

    /**
     * 修改会员管理
     */
    @Log(title = "修改会员状态", businessType = BusinessType.UPDATE)
    @PutMapping("status/{id}/{status}")
    @ApiOperation("修改会员状态")
    public AjaxResult editStatus(@PathVariable("id")Long id,@PathVariable("status")String status)
    {
        tMemberService.updateStatus(id,status);
        return AjaxResult.success();
    }


    /**
     * 修改会员管理
     */
    @Log(title = "修改会员等级", businessType = BusinessType.UPDATE)
    @PutMapping("grade/{id}/{status}")
    @ApiOperation("修改会员等级")
    public AjaxResult editGrade(@PathVariable("id")Long id,@PathVariable("status")String status)
    {
        tMemberService.updateGrade(id,status);
        return AjaxResult.success();
    }


    /**
     * 删除会员管理
     */
    @Log(title = "会员管理", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    @ApiOperation("删除会员管理")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(tMemberService.deleteTMemberByIds(ids));
    }


    @GetMapping("/records/{userId}")
    @ApiOperation("获取查看记录")
    public TableDataInfo getBuyInfoRecord(@PathVariable("userId")Long userId){
        startPage();
        return getDataTable(memberLogService.getCheckRecord(userId));
    }


    @GetMapping("/consume")
    @ApiOperation("获取购买记录")
    public TableDataInfo getBuyInfoRecord(ConsumeLogSearchDto consumeLogSearchDto){
        startPage();
        return getDataTable(memberLogService.getConsumeRecord(consumeLogSearchDto));
    }
}
